/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { SRConfigBuilder } from './SRConfigBuilder';
import { SRConfig } from './SRConfig';
import { SRScene } from './SRScene';
import { SRSceneBuilder } from './SRSceneBuilder';
import { codeGen } from './SRCodeGen';
import path from "path";
import Logger from "./logger";

function isAbsolutePath(path: string): boolean {
    return /^(\/|\\|[A-Za-z]:\\)/.test(path);
}

const args = process.argv.slice(1);
// execute: node gen.js ${absolute/relative path of ttsconfig.json}
args[1] = String(args[1]);
const currentPath = process.cwd();
const ttsConfigPath: string = isAbsolutePath(args[1])? args[1]: path.resolve(currentPath, args[1]);
console.info(`input path of ttsconfig.json5: ${ttsConfigPath}`);
const configBuilder: SRConfigBuilder = new SRConfigBuilder(ttsConfigPath);
try {
    const res: boolean = configBuilder.build()
    if (res) {
        const config: SRConfig = configBuilder.getConfig();
        const sceneBuilder: SRSceneBuilder = new SRSceneBuilder();
        const scene: SRScene = sceneBuilder.build(config);
        // do log4js configure for codeGen
        Logger.configure(config.getLogFilePath(), config.getLogLevel());
        codeGen(scene, config);
    } else {
        console.error('parse ttsconfig.json error!');
    }
} catch(error) {
    console.error(`Fatal ERROR occurred, Class-Transformer exit! Error msg: ${error}`);
}